package com.sinotrans.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinotrans.common.rest.Page;
import com.sinotrans.common.rest.RestEntity;
import com.sinotrans.demo.entity.UserEntity;
import com.sinotrans.demo.repository.UserRepository;
import com.sinotrans.demo.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
	private static final Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private UserService userService;

	@ApiOperation(value = "登录")
	@PostMapping(value = "/login")
	public RestEntity<UserEntity> login(@RequestBody UserEntity entity) {
		RestEntity<UserEntity> rest = new RestEntity<>();
		UserEntity result = null;
		try {
			result = userService.login(entity.getUsername(), entity.getPassword());
			rest.setEntity(result);
		} catch (Exception e) {
			log.error("登录异常", e);
			rest.error(e);
		}
		return rest;
	}

	@PostMapping(value = "/insert")
	public RestEntity<Boolean> insert(@RequestBody List<UserEntity> list) {
		RestEntity<Boolean> rest = new RestEntity<>();
		boolean result = userRepository.saveOrUpdateBatch(list);
		rest.setEntity(result);
		return rest;
	}

	@PostMapping(value = "/update")
	public RestEntity<Boolean> update(@RequestBody List<UserEntity> list) {
		RestEntity<Boolean> rest = new RestEntity<>();
		boolean result = userRepository.saveOrUpdateBatch(list);
		rest.setEntity(result);
		return rest;
	}

	@PostMapping(value = "/delete")
	public RestEntity<Boolean> delete(@RequestBody List<UserEntity> list) {
		RestEntity<Boolean> rest = new RestEntity<>();
		List<String> ids = new ArrayList<>();
		for (UserEntity user : list) {
			ids.add(user.getId());
		}
		boolean result = userRepository.removeByIds(ids);
		rest.setEntity(result);
		return rest;
	}

	@PostMapping(value = "/query")
	public RestEntity<List<UserEntity>> query(@RequestBody UserEntity item) {
		RestEntity<List<UserEntity>> rest = new RestEntity<>();
		QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
		queryWrapper.setEntity(item);
		List<UserEntity> list = userRepository.list(queryWrapper);
		rest.setEntity(list);
		return rest;
	}

	@PostMapping(value = "/page")
	public Page<UserEntity> page(@RequestBody Page<UserEntity> page) {
		QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
		queryWrapper.setEntity(page.getRecords().get(0));
		Page<UserEntity> result = userRepository.page(page, queryWrapper);
		return result;
	}

}
